import { observable, action } from 'mobx';
// eslint-disable-next-line no-unused-vars
import { AxiosInstance } from 'axios';
import { getBlogList, requestFn, getAuthorInfo } from "../lib/httpReq";
import { MAX_COUNT } from "../lib/config";

interface InitialState{
    blogList: Blog[];
    labelList: LabelCount[];
    recentBlogs: Blog[];
    newComments: NewComments[];
    TOTAL: number;
    authorInfo: AuthorInfo;
}

const initialState: InitialState = {
    blogList: [],
    labelList: [],
    recentBlogs: [],
    newComments: [],
    TOTAL: 0,
    authorInfo: {},
};

export class BlogInfo {
    constructor({ blogList, labelList, recentBlogs, newComments, TOTAL, authorInfo }: InitialState = initialState) {
        this.blogList = blogList;
        this.labelList = labelList;
        this.recentBlogs = recentBlogs;
        this.newComments = newComments;
        this.TOTAL = TOTAL;
        this.authorInfo = authorInfo;
    }

    @observable blogList: Blog[];

    @observable labelList: LabelCount[];

    @observable recentBlogs: Blog[];

    @observable newComments: NewComments[];

    @observable authorInfo: AuthorInfo;

    @observable label: string = '';

    @observable blogTotal = 0;

    @observable TOTAL: number;

    @action.bound
    setBlogInfo = (blogInfo: BlogListData) => {
        this.setBlogList(blogInfo.result);
        this.setBlogTotal(blogInfo.total);
    };

    @action.bound
    setRecentBlogs = (blogs: Blog[]) => {
        this.recentBlogs = blogs;
    };

    @action.bound
    setTOTAL = (count: number) => {
        this.TOTAL = count;
    };

    @action.bound
    setLabelList = (labelList: LabelCount[]) => {
        labelList.sort((a, b) => b.count - a.count);
        this.labelList = labelList;
    };

    @action.bound
    setLabel = (label: string, axiosInstance: AxiosInstance) => {
        this.label = label;
        this.getNewBlogList(0, axiosInstance, label);
    };

    @action.bound
    setBlogList = (blogList: Blog[]) => {
        this.blogList = blogList;
    };

    @action.bound
    setBlogTotal = (total: number) => {
        this.blogTotal = total;
    };

    @action.bound
    setAuthorInfo = (info: AuthorInfo) => {
        this.authorInfo = info;
    }

    @action.bound
    setNewComments = (comments: NewComments[]) => {
        this.newComments = comments;
    };

    @action.bound
    getNewBlogList = (pageIdx: number, axiosInstance: AxiosInstance, label: string = this.label) => {
        const curPage = pageIdx * MAX_COUNT;
        requestFn(getBlogList, [curPage, MAX_COUNT, label, axiosInstance], (data) => {
            this.setBlogInfo(data);
            if (pageIdx === 0 && label === '') {
                this.setRecentBlogs(data.result);
            }
            if (label === '') this.setTOTAL(data.total);
        });
    }

    @action.bound
    getAuthorInfo = (axiosInstance: AxiosInstance) => {
        requestFn(getAuthorInfo, [axiosInstance], (data) => {
            this.setAuthorInfo(data);
        });
    };
}

export const BLOG_INFO = 'blogList';
